package com.my.simple.common.util;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

public class HttpServletRequestUtil {

    public static Map<String, Object> getHeaderInfo(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        Enumeration headerNames = request.getHeaderNames();

        while (headerNames.hasMoreElements()) {
            String key = (String) headerNames.nextElement();
            String value = request.getHeader(key);
            map.put(key, value);
        }

        return map;
    }

    public static String getBodyInfo(HttpServletRequest request) throws IOException {
        String bodyInfo = null;
        StringBuilder stringBuilder = new StringBuilder();
        BufferedReader br = request.getReader();
        String str = null;

        while ((str = br.readLine()) != null) {
            stringBuilder.append(str);
        }

        bodyInfo = stringBuilder.toString();
        return bodyInfo;
    }
}
